# Process EA RAW PUBLIC SIGNALS DATA 
rm(list=ls())

(WD <- getwd())
if (!is.null(WD)) setwd(WD)

library(haven)
library(AER)
library(sandwich)
library(lmtest)
library(pracma)
library(stargazer)
library(plm)
library(pracma)
library(DataCombine)
library(jtools)
library(plyr)
library(readxl)
library(foreign)

lagpad <- function(x, k) {
  res <- c(rep(NA, k), x)[1:length(x)]
  return(res)
}

input0 = "/Users/akohl/Dropbox/7 Rational Inattention/_resubmission/_code/_data/_individual_data/_otherrawdata/ea_spf_cpi_ind.rda"

##### CONS DATA #############
data         = read_excel("ea-consensus-cons.xlsx", sheet = "EA_Q61")
data$cons    = as.numeric(data$Mean) 
tmp          = row(data)
tmp          = tmp[,1]
data$qdate   = 2004+(tmp-1)/4

data         = subset(data, select = c(qdate,cons))

save(data,file="ea_raw_data_cons.rda")

##### FM EXP DATA #############
data         = read_excel("ea-inflation-swap.xlsx", sheet = "Data")
data$fmexp   = as.numeric(data$VALUE)
tmp          = row(data)
tmp          = tmp[,1]
data$qdate   = 2004.25+(tmp-1)/4

data         = subset(data, select = c(qdate,fmexp))

save(data,file="ea_raw_data_fmexp.rda")

##### EA SPF DATA #############
load(input0)
data$ID       = data$id
data          = ddply(data,.(qdate),transform, easpf = mean(f2,na.rm = TRUE))  

data          = subset(data, select = c(qdate,easpf))
data          =  aggregate(. ~ qdate, data, mean)

save(data,file="ea_raw_data_easpf.rda")

clear()

##### MERGE #############
load("ea_raw_data_easpf.rda")
data_new    = data
data_series =  c('ea_raw_data_fmexp.rda','ea_raw_data_cons.rda')
for ( i in data_series){
  load(i)
  data_new = merge(data_new, data, all=TRUE)
}

data = data_new[data_new$qdate>=2000.75,]
save(data,file="ea_public_signals_survey.rda")
clear()
